package com.design.fitment.adapter;

/**
 * Created by 娇哥 on 2016/1/16.
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.design.fitment.R;
import com.design.fitment.app.MyApplication;
import com.design.fitment.entity.JieXiDetail;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class JieXiDetailAdapter extends BaseAdapter {

    private List<JieXiDetail.DataEntity.ImgListEntity> objects = new ArrayList<>();
    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    private Context context;
    private LayoutInflater layoutInflater;

    public JieXiDetailAdapter(Context context,List<JieXiDetail.DataEntity.ImgListEntity> objects) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        imageLoader = MyApplication.getApp().getImageLoader();
        options = MyApplication.getApp().getImageOptions();
        this.objects = objects;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public JieXiDetail.DataEntity.ImgListEntity getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.jiexi_detail_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.ivJiexiDetailItem = (ImageView) convertView.findViewById(R.id.iv_jiexi_detail_item);
            viewHolder.tvListItem = (TextView) convertView.findViewById(R.id.tv_list_item);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        imageLoader.displayImage(objects.get(position).getImgUrl(), viewHolder.ivJiexiDetailItem, options);
        if (objects.get(position).getComment()!=null){
            viewHolder.tvListItem.setVisibility(View.VISIBLE);
            viewHolder.tvListItem.setText(objects.get(position).getComment());
        }else{
            viewHolder.tvListItem.setVisibility(View.GONE);
        }
        return convertView;
    }


    protected class ViewHolder {
        private ImageView ivJiexiDetailItem;
        private TextView tvListItem;
    }
}
